package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.conmmon.R;
import com.itheima.domain.Employee;
import com.itheima.service.EmployeeService;
import jdk.nashorn.internal.ir.CallNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author liwanhu
 * @datetime 2021/10/24 19:13
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("登录{}", employee.toString());
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username", employee.getUsername());
        Employee one = employeeService.getOne(qw);
        if (one == null) {
            return R.error("用户不存在");
        }
        if (!one.getPassword().equals(password)) {
            return R.error("登陆失败");
        }
        if (one.getStatus() == 0) {
            return R.error("该用户已被禁用");
        }
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.success("退出登录");
    }

    /**
     * 添加员工
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        //设置初始密码,并对其进行MD5的加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置创建时间
        /*employee.setCreateTime(LocalDateTime.now());
        //设置修改时间
        employee.setUpdateTime(LocalDateTime.now());
        //获取当前登录的用户
        Long eId = (Long) request.getSession().getAttribute("employee");
        //设置创建人id
        employee.setCreateUser(eId);
        employee.setUpdateUser(eId);*/
        //调用添加员工的方法
        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    /**
     * 员工分页查询,StringUtils.isNotBlank()返回参数是否不为空时
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String name) {
        //页面发送ajax请求，将分页查询参数(page、pageSize、name)提交到服务端
        Page pages = new Page(page, pageSize);
        //构造条件构造器
        QueryWrapper<Employee> queryWrapper = new QueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        //添加排序条件
        queryWrapper.orderByDesc("Update_time");
        //执行查询
        employeeService.page(pages, queryWrapper);
        return R.success(pages);
    }

    /**
     * 根据id修改员工信息
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        //获取当前登录状态
        Long id = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(id);
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("修改员工信息");
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("查无此信息");
    }
}
